package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.MmoConstant
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.SettingAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.UserBean
import com.horoscope.astrology.zodiac.signs.free.infc.OnItemClickedListener
import com.horoscope.astrology.zodiac.signs.free.utils.*
import java.util.*

/**
 * 个人页面
 */
class SettingActivity : BaseActivity() {

    private var tvUserName: TextView? = null
    private var tvConstellationName: TextView? = null
    private var tvBirthday: TextView? = null
    private var ivConstellationIcon: ImageView? = null
    private var list: RecyclerView? = null
    private var mLinearLayoutManager: LinearLayoutManager? = null
    private var profileHome: View? = null
    var mUserBean: UserBean? = null
    private var date: String? = null

    private val dataList =
        arrayOf("Feedback", "Rating us", "Privacy Policy", "Terms of Service", "Current Version")
    private val imageList = intArrayOf(
        R.mipmap.icon_set_feedback,
        R.mipmap.icon_set_ratingus,
        R.mipmap.icon_set_privacy_policy,
        R.mipmap.icon_set_terms_service,
        R.mipmap.icon_set_version
    )

    private var lastTime: Long = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)
        initTitleBar(
            this,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            ""
        )
        initView()
    }

    private fun initView() {
        profileHome = findViewById(R.id.linear_layout_profile)
        ivConstellationIcon = findViewById(R.id.iv_title_icon)
        tvUserName = findViewById(R.id.user_name)
        tvConstellationName = findViewById(R.id.constellation_name)
        tvBirthday = findViewById(R.id.user_birthday)
        loadNewData()
        list = findViewById(R.id.list_item)
        mLinearLayoutManager = LinearLayoutManager(this)
        list!!.layoutManager = mLinearLayoutManager
        val version = SysUtils.getAppVersion()

        //添加自定义分割线
        //val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        //divider.setDrawable(this.getDrawable(R.drawable.custom_divider)!!)
        //list!!.addItemDecoration(divider)

        val adapter = SettingAdapter(this@SettingActivity, dataList, imageList, version)
        list!!.adapter = adapter


        adapter.setOnItemClickedListener(object : OnItemClickedListener {

            override fun onItemClick(view: View, position: Int, string: String) {
                when (position) {
                    0 -> {
                        // Me 页点击的位置
                        FlurryEventUtil.putDataToEvent(EventType.TYPE_ME_CLICK, "type", "Feedback")
                        SysUtils.startFeedback(this@SettingActivity)
                    }
                    1 -> {
//                        FlurryEventUtil.putDataToEvent(EventType.TYPE_ME_CLICK, "type", "Rating")
                        ProjectUtils.goToRating(this@SettingActivity)
                    }
                    2 -> {
                        FlurryEventUtil.putDataToEvent(EventType.TYPE_ME_CLICK, "type", "Policy")
                        val intentPrivacy = WebViewActivity
                            .generateWebViewIntent(
                                this@SettingActivity,
                                MmoConstant.POLICY_URL,
                                false,
                                getString(R.string.text_privacy_policy)
                            )
                        startActivity(intentPrivacy)
                    }
                    3 -> {
                        FlurryEventUtil.putDataToEvent(EventType.TYPE_ME_CLICK, "type", "Terms")
                        val intentService = WebViewActivity.generateWebViewIntent(
                            this@SettingActivity, MmoConstant.TERMS_URL,
                            false,
                            getString(R.string.text_terms)
                        )
                        startActivity(intentService)
                    }
                }
            }
        })


        profileHome!!.setOnClickListener {
            if (System.currentTimeMillis() - lastTime > 400) {
                FlurryEventUtil.putDataToEvent(EventType.TYPE_ME_CLICK, "type", "Profile")
                val intent = Intent(this, ProfileActivity::class.java)
                startActivityForResult(intent, 1)
                lastTime = System.currentTimeMillis()
            }
        }
    }

    // fragment会预加载，用户信息更新后要重新获取userbean来跟新UI
    private fun loadNewData() {
        mUserBean = AppSettingManager.readObject("user_info")
        if (mUserBean == null) {
            mUserBean = UserBean()
        }
        date = mUserBean!!.birthday
        val month = ConstellationUtil.getMonthFromStringDate(date)
        val day = ConstellationUtil.getDayFromStringDate(date)
        ivConstellationIcon!!.setImageResource(
            ConstellationUtil.getConstellationDrawable(month - 1, day, 0)
        )

        tvUserName!!.text = mUserBean!!.name

        tvConstellationName!!.text = mUserBean!!.constellationName


        tvBirthday!!.text = mUserBean!!.birthday
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK_PROFILE) {
            val mUserBean = AppSettingManager.readObject("user_info")
            val date = mUserBean!!.birthday
            tvUserName!!.text = Objects.requireNonNull(mUserBean).name
            tvConstellationName!!.text = Objects.requireNonNull(mUserBean).constellationName
            tvBirthday!!.text = mUserBean.birthday
            ivConstellationIcon!!.setImageResource(
                ConstellationUtil.getConstellationDrawable(
                    ConstellationUtil.getMonthFromStringDate(date) - 1,
                    ConstellationUtil.getDayFromStringDate(date), 0
                )
            )
        }
    }

    companion object {
        private const val RESULT_OK_PROFILE = 4
    }

}
